<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="goodsname">
      <el-input v-model="dataForm.goodsname" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="produceplace">
      <el-input v-model="dataForm.produceplace" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="size">
      <el-input v-model="dataForm.size" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodspackage">
      <el-input v-model="dataForm.goodspackage" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="productcode">
      <el-input v-model="dataForm.productcode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="promitcode">
      <el-input v-model="dataForm.promitcode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="description">
      <el-input v-model="dataForm.description" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="price">
      <el-input v-model="dataForm.price" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="number">
      <el-input v-model="dataForm.number" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="dangernum">
      <el-input v-model="dataForm.dangernum" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsimg">
      <el-input v-model="dataForm.goodsimg" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="available">
      <el-input v-model="dataForm.available" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="providerid">
      <el-input v-model="dataForm.providerid" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          goodsname: '',
          produceplace: '',
          size: '',
          goodspackage: '',
          productcode: '',
          promitcode: '',
          description: '',
          price: '',
          number: '',
          dangernum: '',
          goodsimg: '',
          available: '',
          providerid: ''
        },
        dataRule: {
          goodsname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          produceplace: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          size: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodspackage: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productcode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          promitcode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dangernum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsimg: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          available: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          providerid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/warehouse/goods/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsname = data.goods.goodsname
                this.dataForm.produceplace = data.goods.produceplace
                this.dataForm.size = data.goods.size
                this.dataForm.goodspackage = data.goods.goodspackage
                this.dataForm.productcode = data.goods.productcode
                this.dataForm.promitcode = data.goods.promitcode
                this.dataForm.description = data.goods.description
                this.dataForm.price = data.goods.price
                this.dataForm.number = data.goods.number
                this.dataForm.dangernum = data.goods.dangernum
                this.dataForm.goodsimg = data.goods.goodsimg
                this.dataForm.available = data.goods.available
                this.dataForm.providerid = data.goods.providerid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/warehouse/goods/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'goodsname': this.dataForm.goodsname,
                'produceplace': this.dataForm.produceplace,
                'size': this.dataForm.size,
                'goodspackage': this.dataForm.goodspackage,
                'productcode': this.dataForm.productcode,
                'promitcode': this.dataForm.promitcode,
                'description': this.dataForm.description,
                'price': this.dataForm.price,
                'number': this.dataForm.number,
                'dangernum': this.dataForm.dangernum,
                'goodsimg': this.dataForm.goodsimg,
                'available': this.dataForm.available,
                'providerid': this.dataForm.providerid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
